﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CrazyTalk.BLL;
using CrazyTalk.Common;
using CrazyTalk.Models;
using CrazyTalk.Web.UI;

namespace CrazyTalk.Web.Actions
{
    /// <summary>
    /// Topic 的摘要说明
    /// </summary>
    public class Topic : UserHttpHandler
    {
        public Topic()
        {
            MenuId = 104;
        }
        public override void ProcessRequest(HttpContext context)
        {
            InHasPermission();
            context.Response.ContentType = "text/plain";
            string cmd = Query("cmd");
            if (string.IsNullOrEmpty(cmd))
                cmd = Form("cmd");
            if (string.IsNullOrEmpty(cmd))
            {
                context.Response.Write("访问错误");
                return;
            }
            switch (cmd)
            {
                case "add":
                    Add();
                    break;
                case "del":
                    Delete();
                    break;
                case "getIndexList":
                    GetIndexList();
                    break;
            }
        }
        private void Add()
        {
            try
            {
                string tags = Form("tags").Trim();
                string linkUrl = Form("linkUrl").Trim();
                string content = Form("content").Trim();

                if (string.IsNullOrEmpty(tags))
                {
                    Response.Write(JSON.GetJsonMessage(false, "标签输入有误"));
                    return;
                }
                string[] tagsOri = tags.Split(',');
                List<string> tagsInput = new List<string>();
                foreach (string s in tagsOri)
                {
                    string tagDeal = s.Trim();
                    if (ValidationUtil.IsValidStringRangeMix(tagDeal, 1, 100) && !tagsInput.Contains(tagDeal))
                    {
                        tagsInput.Add(tagDeal);
                    }
                }
                if (tagsInput.Count == 0)
                {
                    Response.Write(JSON.GetJsonMessage(false, "输入标签不合法"));
                    return;
                }

                string[] linkUrlsOri = linkUrl.Split(new[] { ',', '\n' });
                List<string> linkUrlsInput = new List<string>();
                foreach (string s in linkUrlsOri)
                {
                    string linkUrlDeal = s.Trim();
                    if (ValidationUtil.IsValidStringRangeMix(linkUrlDeal, 5, 200) && !linkUrlsInput.Contains(linkUrlDeal))
                    {
                        linkUrlsInput.Add(linkUrlDeal);
                    }
                }

                TopicBase model = new TopicBase();
                model.Content = content;
                model.Editor = model.CreatorId = CurUserInfo.seqno;
                model.EditDate = model.CreateDate = DateTime.Now;
                int topicBaseId = TopicBaseBLL.Instance.Add(model);
                if (topicBaseId < 1)
                {
                    Response.Write(JSON.GetJsonMessage(false, "录入失败"));
                    return;
                }
                TopicExtendBLL.Instance.Add(new TopicExtend { TopicBaseId = topicBaseId });
                foreach (string s in tagsInput)
                {
                    TopicTagBLL.Instance.Add(new TopicTag { TagId = TagBLL.Instance.GetTagId(s), TopicBaseId = topicBaseId });
                }

                foreach (string s in linkUrlsInput)
                {
                    TopicUrlMatchBLL.Instance.Add(new TopicUrlMatch { Url = s, TopicBaseId = topicBaseId });
                }
                Response.Write(JSON.GetJsonMessage(true, "录入成功"));
            }
            catch (Exception ex)
            {
                Response.Write(JSON.GetJsonMessage(false, "录入失败，" + ex.Message));
            }
        }

        /// <summary>
        /// 删除用户消息逻辑
        /// </summary>
        private void Delete()
        {
            string keyId = Query("id");
            if (string.IsNullOrEmpty(keyId))
            {
                CurContext.Response.Write("传入参数有误");
            }
            else
            {
                //只能删除发给自己的信息
                string errorMsg = string.Empty;

                //if (CmUserMessageBLL.Instance.Delete(ConvertUtil.ToInt(keyId), CurUserInfo.UserId, CurUserInfo.UserId))
                //{
                //    CurContext.Response.Write(string.Format("已成功删除信息", keyId));
                //}
                //else
                //{
                //    CurContext.Response.Write(string.Format("删除信息失败", keyId));
                //}
            }
        }
        
        private void GetIndexList()
        {
            int pageIndex = QueryToInt("page");
            int pageSize = QueryToInt("rows");
            if (pageSize == 0)
                pageSize = 10;

            Hashtable list = TopicBaseBLL.Instance.GetIndexList(pageIndex, pageSize);
            String json = JSON.Encode(list);
            Response.Write(json);
        }
    }
}